package com.yanggeng.demo.beginnerguidedemo.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import com.yanggeng.demo.beginnerguidedemo.R;
import com.yanggeng.demo.beginnerguidedemo.adapter.SimpleAdapter;
import com.yanggeng.demo.beginnerguidedemo.utils.MyDecoration;
import com.yanzhenjie.recyclerview.swipe.SwipeMenu;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuBridge;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuCreator;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuItem;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuItemClickListener;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuRecyclerView;
import com.yanzhenjie.recyclerview.swipe.touch.OnItemMoveListener;
import com.yanzhenjie.recyclerview.swipe.widget.DefaultItemDecoration;

import java.util.ArrayList;
import java.util.List;

public class SwipeRecyclerViewActivity extends BaseActivity {

    private SwipeMenuRecyclerView swipeMenuRecyclerView;
    private SimpleAdapter adapter;
    private int page = 0, size = 15;
    private List<String> data = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_swipe_recyclerview);
        setTitle(true, "SwipeRecyclerView");
        swipeMenuRecyclerView = (SwipeMenuRecyclerView) findViewById(R.id.swipe_recyclerview);
        adapter = new SimpleAdapter(context, data);
        swipeMenuRecyclerView.useDefaultLoadMore();
        swipeMenuRecyclerView.addItemDecoration(new DefaultItemDecoration(Color.RED, ViewGroup.LayoutParams.MATCH_PARENT, 20));
//        swipeMenuRecyclerView.setAutoLoadMore(true);
        swipeMenuRecyclerView.setLoadMoreListener(new SwipeMenuRecyclerView.LoadMoreListener() {
            @Override
            public void onLoadMore() {
                page++;
                loadData(page, size);
            }
        });
        swipeMenuRecyclerView.setLayoutManager(new LinearLayoutManager(context));
        swipeMenuRecyclerView.setSwipeMenuCreator(new SwipeMenuCreator() {
            @Override
            public void onCreateMenu(SwipeMenu swipeLeftMenu, SwipeMenu swipeRightMenu, int viewType) {
                int width = getResources().getDimensionPixelSize(R.dimen.dp_70);
                SwipeMenuItem deleteItem = new SwipeMenuItem(context)
                        .setBackgroundColor(Color.RED)
                        .setText("delete")
                        .setTextColor(Color.WHITE)
                        .setTextSize(15)
                        .setWidth(width)
                        .setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
                swipeRightMenu.addMenuItem(deleteItem);
                SwipeMenuItem editItem = new SwipeMenuItem(context)
                        .setText("edit")
                        .setTextColor(Color.WHITE)
                        .setBackgroundColor(Color.BLUE)
                        .setTextSize(15)
                        .setWidth(width)
                        .setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
                swipeRightMenu.addMenuItem(editItem);
            }
        });
        swipeMenuRecyclerView.setSwipeMenuItemClickListener(new SwipeMenuItemClickListener() {
            @Override
            public void onItemClick(SwipeMenuBridge menuBridge) {
                menuBridge.closeMenu();
                int position = menuBridge.getAdapterPosition();
                Log.e(TAG, "item position = " + position + ", menu position = " + menuBridge.getPosition());
                if (menuBridge.getPosition() == 0) {
                    data.remove(position);
                    adapter.notifyItemRemoved(position);
                }
            }
        });
        swipeMenuRecyclerView.setAdapter(adapter);
        loadData(page, size);
    }

    private void loadData(int page, int size) {

        for (int i = 0; i < size; i++) {
            data.add("NO." + page + i);
        }
        adapter.notifyData(data);
        swipeMenuRecyclerView.loadMoreFinish(false, true);
    }
}
